/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/

This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, 
including commercial applications, and to alter it and redistribute it freely, 
subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#include "btBoxCylinderCollisionAlgorithm.h"
#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
#include "BulletCollision/CollisionShapes/btCylinderShape.h"
#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
#include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
#include "collision_libccd.h"
//#include <stdio.h>

//extern static int ccdCollide(btCollisionObject* o1, btCollisionObject* o2, int flags,
//                      ContactGeom *contact, int skip,
//                      void *obj1, ccd_support_fn supp1, ccd_center_fn cen1,
//                      void *obj2, ccd_support_fn supp2, ccd_center_fn cen2);

btBoxCylinderCollisionAlgorithm::btBoxCylinderCollisionAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* col0Wrap,const btCollisionObjectWrapper* col1Wrap, bool isSwapped)
: btActivatingCollisionAlgorithm(ci,col0Wrap,col1Wrap),
m_ownManifold(false),
m_manifoldPtr(mf),
//m_isSwapped(isSwapped)
m_isSwapped(true)
{
	const btCollisionObjectWrapper* boxObjWrap = m_isSwapped? col0Wrap : col1Wrap;
	const btCollisionObjectWrapper* cylinderObjWrap = m_isSwapped? col1Wrap : col0Wrap;
		
	if (!m_manifoldPtr && m_dispatcher->needsCollision(boxObjWrap->getCollisionObject(),cylinderObjWrap->getCollisionObject()))
	{
		m_manifoldPtr = m_dispatcher->getNewManifold(boxObjWrap->getCollisionObject(),cylinderObjWrap->getCollisionObject());
		m_ownManifold = true;
	}
}


btBoxCylinderCollisionAlgorithm::~btBoxCylinderCollisionAlgorithm()
{
	if (m_ownManifold)
	{
		if (m_manifoldPtr)
			m_dispatcher->releaseManifold(m_manifoldPtr);
	}
}



void btBoxCylinderCollisionAlgorithm::processCollision (const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut)
{
	(void)dispatchInfo;
	(void)resultOut;
	if (!m_manifoldPtr)
		return;
	resultOut->setPersistentManifold(m_manifoldPtr);
	
	const btCollisionObjectWrapper* boxObjWrap = m_isSwapped? body0Wrap : body1Wrap;
	const btCollisionObjectWrapper* cylinderObjWrap = m_isSwapped? body1Wrap : body0Wrap;
	
	btVector3 pOnCylinder;
	btVector3 normalOnSurfaceB;
	btScalar penetrationDepth;
	//btVector3 sphereCenter = cylinderObjWrap->getWorldTransform().getOrigin();
	//const btCylinderShape* cylinder0 = (const btCylinderShape*)cylinderObjWrap->getCollisionShape();
	//btScalar radius = cylinder0->getRadius();
	//btScalar maxContactDistance = m_manifoldPtr->getContactBreakingThreshold();

	//const btCollisionObject* o1 = body0Wrap->getCollisionObject();
	//const btCollisionObject* o2 = body1Wrap->getCollisionObject();
	int fakeflags(8), _skip(8);
	ContactGeom contact;
	ccd_box_t box1;
    ccd_cyl_t cyl2;
	ccdGeomToBox(boxObjWrap->getCollisionObject(), &box1);
    ccdGeomToCyl(cylinderObjWrap->getCollisionObject(), &cyl2);

	if (ccdCollide(boxObjWrap->getCollisionObject(), cylinderObjWrap->getCollisionObject(), fakeflags, &contact, _skip, &box1, ccdSupportBox, ccdCenter, &cyl2, ccdSupportCyl, ccdCenter) == 1)
	{
		/// report a contact. internally this will be kept persistent, and contact reduction is done
		penetrationDepth = -contact.depth;
		pOnCylinder = contact.pos;
		normalOnSurfaceB = contact.normal;
		resultOut->addContactPoint(normalOnSurfaceB, pOnCylinder, penetrationDepth);

		/*dispatchInfo.m_debugDraw->drawLine(contact.pos, contact.pos+normalOnSurfaceB*100, btVector3(0xff, 0, 0), btVector3(0, 0, 0));
		btTransform tempTrans(btQuaternion(cyl2.o.rot.q[0], cyl2.o.rot.q[1], cyl2.o.rot.q[2], cyl2.o.rot.q[3]));
		tempTrans.setOrigin(btVector3(cyl2.o.pos.v[0], cyl2.o.pos.v[1], cyl2.o.pos.v[2]));
		dispatchInfo.m_debugDraw->drawSphere(pOnCylinder,0.5, btVector3(0, 0, 0xff));
		//dispatchInfo.m_debugDraw->drawContactPoint(pOnCylinder, normalOnSurfaceB, -1000, 1000000,btVector3(0xff, 0, 0));
		dispatchInfo.m_debugDraw->drawCylinder(	cyl2.radius, cyl2.height, 2, tempTrans, btVector3(0xff, 0xff, 0));

		tempTrans = btTransform(btQuaternion(cyl1.o.rot.q[0], cyl1.o.rot.q[1], cyl1.o.rot.q[2], cyl1.o.rot.q[3]));
		tempTrans.setOrigin(btVector3(cyl1.o.pos.v[0], cyl1.o.pos.v[1], cyl1.o.pos.v[2]));
		dispatchInfo.m_debugDraw->drawCylinder(	cyl1.radius, cyl1.height, 2, tempTrans, btVector3(0, 0, 0xff));*/
	}
//#ifndef CLEAR_MANIFOLD
//	resultOut->refreshContactPoints();
//#endif //CLEAR_MANIFOLD
	if (m_ownManifold)
	{
		if (m_manifoldPtr->getNumContacts())
		{
			resultOut->refreshContactPoints();
		}
	}

}

btScalar btBoxCylinderCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* col0,btCollisionObject* col1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
{
	(void)resultOut;
	(void)dispatchInfo;
	(void)col0;
	(void)col1;

	//not yet
	return btScalar(1.);
}





